﻿using System;
using System.Diagnostics.Contracts;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Threading;
using System.Xml;
using OpenLS.Drawing;

namespace OpenLS.Spreadsheet.Serialization
{
    class DefaultXamlElementResolver: IXamlElementResolver
    {
        public IAnchored Resolve(Picture picture)
        {
            Contract.Requires(picture != null);
            Contract.Ensures(Contract.Result<IAnchored>() != null);
            if (picture == null)
                return picture;
            if (picture.PictureNonVisual == null || picture.PictureNonVisual.NonVisualDrawingProps == null)
                return picture;
            string description = picture.PictureNonVisual.NonVisualDrawingProps.Description;
            if (string.IsNullOrEmpty(description))
                return picture;
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(description);
/*                FrameworkElement content = null;
                SimpleAction action = () => content = (FrameworkElement) XamlReader.Parse(description);
                if (dispatcher != null)
                    dispatcher.Invoke(action);
                else
                    action();
  */              return new XamlElement(description) {};
            }
            catch (Exception)
            {
                return picture;
            }

        }
    }
}